package com.chen.manager.controller;

import com.chen.manager.service.SysRoleService;
import com.chen.manager.service.SysUserRoleService;
import com.chen.model.dto.system.SysRoleDto;
import com.chen.model.entity.system.SysRole;
import com.chen.model.vo.common.Result;
import com.chen.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author cgj
 * @date 2/12/25 8:32 AM
 */
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserRoleService sysUserRoleService;
    /**
     * 查询所有和指定id所拥有的用户角色
     *
     */
    @GetMapping("/selectAll/{uid}")
    public Result<List<SysRole>> selectAll(@PathVariable Long uid){
        HashMap<String, Object> resultMap = new HashMap<>();
        //查询所有
        List<SysRole> sysRoleList=sysRoleService.selectAll();
        resultMap.put("sysRoleList",sysRoleList);
        //查询指定id用户所拥有的角色
       List<Long> sysUserRoles= sysUserRoleService.findRoleByUserId(uid);
      resultMap.put("sysUserRoles",sysUserRoles);
        return  Result.build(resultMap,ResultCodeEnum.SUCCESS);
    }
    /**
     * 删除角色
     */
    @DeleteMapping("/delete/{id}")
    public Result removeSysRole(@PathVariable Integer id){
        sysRoleService.removeSysRole(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    /**
     * 新增角色
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody SysRole sysRole){
        sysRoleService.edit(sysRole);

        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


//    /admin/system/sysRole/save
    /**
     * 新增角色
     */
    @PostMapping("save")
    public  Result savaRole(@RequestBody SysRole sysRole,@RequestHeader(name = "token") String token){
        Integer row=sysRoleService.savaRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    /**
     *
     * @param sysRoleDto   模糊查询条件
     * @param pageNum    当前页
     * @param pageSize   每一页数据的条数
     * @return
     */
    @GetMapping("/findAll/{pageNum}/{pageSize}")
    public Result<PageInfo<SysRole>> findAllByPage(SysRoleDto sysRoleDto,
                                                   @PathVariable Integer pageNum,
                                                   @PathVariable Integer pageSize,
                                                   @RequestHeader(name = "token") String token){
        PageInfo<SysRole> pageInfo =sysRoleService.findAllByPage(sysRoleDto,pageNum,pageSize);

        return  Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }
}
